package com.SMP.service;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class BroadcastService {

    public static final String PATH = "D:/photo/";

    /**
     * @author WYHY
     * 获取资源
     * */
    public List<String> getFileName(String url) {
        List<String> filename=new ArrayList<String>();
        File f = new File(PATH+url);
        if (!f.exists()) {
            System.out.println(PATH+url + " not exists");
            filename.add("目录不存在");
            return filename;
        }else{
            File fa[] = f.listFiles();
            for (int i = 0; i < fa.length; i++) {
                File fs = fa[i];
                if (fs.isDirectory()) {
                    System.out.println(fs.getName() + " [目录]");
                } else {
//                System.out.println(fs.getName());
                    filename.add("/photo/"+url + fs.getName());
                }
            }
            return filename;
        }
    }

    /**
     * @Description: 删除文件
     * @Param: [fileurl]
     * @return: java.lang.String
     * @Author: WYHY
     * @Date: 2018/10/24
     */
    public boolean deleteFile(String fileurl){
        File file=new File(PATH+fileurl);
        if (file.isDirectory()){
            return false;
        }else{
            file.delete();
            return true;
        }
    }

    /**
     * @Description: 上传附件
     * @Param: [file, fileType, annexId, cardId ,request]
     * @Author: WYHY
     * @Date: 2018/9/7
     */
    public String uploadFile(MultipartFile file,String url) {
        int num=0;
        String name="";
        if (file != null) {// 判断上传的文件是否为空
            String type = null;// 文件类型
            String fileName = file.getOriginalFilename();// 文件原名称
            type = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toUpperCase() : null;
            if (type != null && (type.equals("mp4") || type.equals("JPG") || type.equals("PNG") || type.equals("GIF"))) {// 判断文件类型
                String filePath = PATH+url;
                if (filePath.equals("")) {
                    name+=fileName+" 文件类型错误！\n";
                } else {
                    File writeFile = new File(filePath + fileName);
                    try {
                        //文件写入磁盘
                        while (writeFile.exists()) {
                            writeFile = new File(filePath + fileName);
                        }
                        file.transferTo(writeFile);
                        num++;
                    } catch (IOException e) {
                        e.printStackTrace();
                        name+=fileName+" 文件保存失败！\n";
                    }
                }
            } else {
                name+=fileName+" 文件格式错误！\n";
            }
        }
        return "上传成功";
    }

    /**
     *  移动文件到指定目录
     */
    public void moveFile(String fromPath,String toPath) {
        fromPath=PATH+fromPath;
        toPath=PATH+toPath;
        File fromFolder = new File(fromPath);
        File[] fromFiles = fromFolder.listFiles();
        if (fromFiles == null) {
            return;
        }
        File toFolder = new File(toPath);
        if (!toFolder.exists()) {
            toFolder.mkdirs();
        }
        for (int i = 0; i < fromFiles.length; i++) {
            File file = fromFiles[i];
            if (file.isDirectory()) {
                moveFile(file.getPath(), toPath + "\\" + file.getName());
                //亦可删除
                file.delete();
            }
            if (file.isFile()) {
                File toFile = new File(toFolder + "\\" + file.getName());
                if (toFile.exists()) {
                    //亦可删除
                    toFile.delete();
                }
                //移动文件
                file.renameTo(toFile);
            }

        }
    }

}
